package solutions.lib;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    private String format(int height) {
        StringBuilder sbT = new StringBuilder();
        int p = height;
        while (p-- > 0) {
            sbT.append('\t');
        }
        StringBuilder r = new StringBuilder();
        r.append(sbT).append(val).append('\n');
        if (left == null && right == null) {
            return r.toString();
        }
        sbT.append('\t');
        if (left == null) {
            r.append(sbT).append("null").append('\n');
        } else {
            r.append(left.format(height + 1));
        }
        if (right == null) {
            r.append(sbT).append("null").append('\n');
        } else {
            r.append(right.format(height + 1));
        }
        return r.toString();
    }

    @Override
    public String toString() {
        return format(0);
    }
}
